package com.mazaiting.redeye.module.me.dd

import android.content.Context
import android.content.Intent
import android.opengl.Visibility
import android.os.Bundle
import android.os.PersistableBundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import cn.jpush.android.api.JPushInterface
import com.mazaiting.accessibility.base.AccessibilityOperator
import com.mazaiting.accessibility.base.CustomAccessibilityService
import com.mazaiting.accessibility.util.AccessibilityUtil
import com.mazaiting.easy.app.IApplicationComponent
import com.mazaiting.easy.base.activity.BaseToolbarActivity
import com.mazaiting.log.L
import com.mazaiting.redeye.R
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.event.AliasEnum
import com.mazaiting.redeye.event.AliasEvent
import kotlinx.android.synthetic.main.activity_dingding.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 钉钉服务页面
 */
class DdActivity : BaseToolbarActivity<DdPresenter>(), DdContract.View {

    companion object {
        /**
         * 开启新实例
         * @param context 上下文
         */
        fun newInstance(context: Context) {
            val intent = Intent(context, DdActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        // 设置无障碍服务
        AccessibilityOperator.INSTANCE.setService(DingDingService())
    }

    /**
     * 检测服务状态
     */
    private fun checkServiceStatus() {
        // 检测服务是否开启
        val isStart = AccessibilityUtil.isAccessibilitySettingsOn(this, CustomAccessibilityService::class.java.name)
        tv_start.text = if (isStart) getString(R.string.start) else getString(R.string.stop)
    }

    override fun getContentLayout(): Int = R.layout.activity_dingding

    override fun inject(applicationComponent: IApplicationComponent?) {
        DaggerDdComponent
            .builder()
            .applicationComponentImpl(applicationComponent as ApplicationComponentImpl)
            .build()
            .inject(this)
    }

    override fun bindView(view: View?, savedInstanceState: Bundle?) {
        super.bindView(view, savedInstanceState)
        EventBus.getDefault().register(this)

        initListener()
    }

    /**
     * 初始化点击事件监听
     */
    private fun initListener() {

        // 设置刷新时间
        tv_tip_start.setOnClickListener {
            checkServiceStatus()
        }

        // 设置点击事件
        tv_start.setOnClickListener {
            when (tv_start.text) {
                // 显示已开启
                getString(R.string.start) -> {}
                // 显示未开启
                getString(R.string.stop) -> { AccessibilityUtil.jumpToSettingPage(this) }
            }
        }

        // 点击设置别名
        btn_alias.setOnClickListener {
            JPushInterface.setAlias(this, AliasEnum.SET.ordinal, "17690823157")
        }
    }

    override fun initData() {
        // 检测服务状态
        checkServiceStatus()
        // 获取别名
        JPushInterface.getAlias(this, AliasEnum.GET.ordinal)
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    /**
     * 别名事件
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAliasEvent(event: AliasEvent) {
//        L.d("event: $event")
        // 获取推送消息
        val message = event.message
        // 别名事件分别处理
        when(AliasEnum.getEnum(message.sequence)) {
            AliasEnum.GET -> onGetAlias(message.alias)
            AliasEnum.SET -> onSetAlias(message.alias)
            AliasEnum.DELETE -> L.d("DELETE")
        }
    }

    /**
     * 设置别名
     * @param alias 别名
     */
    private fun onSetAlias(alias: String?) {
        // 检测别名是否为空
        if (TextUtils.isEmpty(alias)) {
            btn_alias.visibility = View.VISIBLE
            tv_alias.visibility = View.GONE
            Toast.makeText(this, "设置别名失败", Toast.LENGTH_SHORT).show()
        } else {
            btn_alias.visibility = View.GONE
            tv_alias.visibility = View.VISIBLE
            tv_alias.text = alias
            Toast.makeText(this, "设置别名成功", Toast.LENGTH_SHORT).show()
        }
    }

    /**
     * 获取别名
     * @param alias 别名
     */
    private fun onGetAlias(alias: String?) {
        // 检测别名是否为空
        if (TextUtils.isEmpty(alias)) {
            btn_alias.visibility = View.VISIBLE
            tv_alias.visibility = View.GONE
        } else {
            btn_alias.visibility = View.GONE
            tv_alias.visibility = View.VISIBLE
            tv_alias.text = alias
        }
    }
}
